//
//  MKRefreshHeaderView.m
//  Category
//
//  Created by Burn on 2020/1/6.
//  Copyright © 2020 menksoft. All rights reserved.
//

#import "MKRefreshHeaderView.h"
#import "UIView+Extension.h"
#import "RefreshConst.h"
#import "UIScrollView+Extension.h"
@interface MKRefreshHeaderView ()

@property(weak,nonatomic) id target;

@property(assign,nonatomic) SEL action;


@property(assign,nonatomic) BOOL panState;

@property(assign,nonatomic) CGFloat offsety;


@end


@implementation MKRefreshHeaderView

+ (instancetype)headerWithRefreshingTarget:(id)target refreshingAction:(SEL)action{
    
    return [[[MKRefreshHeaderView alloc]init]headerWithRefreshingTarget:target refreshingAction:action];
}

- (void)endRefreshing{
    
    [UIView animateWithDuration:.25 animations:^{
        self.scrollView.insetTop = 0;
    }];
    self.state = RefreshIdle;
    self.panState = NO;
}

- (instancetype)headerWithRefreshingTarget:(id)target refreshingAction:(SEL)action{
    
    MKRefreshHeaderView *header = [[MKRefreshHeaderView alloc]init];
    return header;
}




- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(0, -MKRefreshContentInsetSpacing, self.width, self.height);
}

- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change{
    [super scrollViewContentOffsetDidChange:change];
    
    
    if (self.panState && (self.state == RefreshIdle || self.state == RefreshPulling)) {
        if (self.offsety <= -MKRefreshContentInsetSpacing) {
            self.state = RefreshRefreshing;
            [UIView animateWithDuration:.25 animations:^{
                self.scrollView.insetTop = MKRefreshContentInsetSpacing;
            }];
            [self.target performSelector:self.action];
        }
    }
    
    
    // !!!: 给控件赋值
    [self protocolSend];

    if (self.state == RefreshRefreshing) {
        // !!!: 上面的间距 0  到 MKRefreshContentInsetSpacing时,上下滑动 sectionHeader 位置调整
        if (self.scrollView.offsetY < 0 && self.scrollView.offsetY >= -MKRefreshContentInsetSpacing) {
//            NSLog(@"展开--%f---%d",self.scrollView.offsetY,self.panState);
            self.scrollView.insetTop = MKRefreshContentInsetSpacing - (MKRefreshContentInsetSpacing - fabs(self.scrollView.offsetY));
            
        }
        // !!!: 向上推 headerView 完全进去时 sectionHeader 位置调整
        else if (self.scrollView.contentOffset.y > 0) {
            self.scrollView.insetTop = 0;
        }
        return;
    }

}

- (void)scrollViewContentSizeDidChange:(NSDictionary *)change{
    [super scrollViewContentSizeDidChange:change];
    
    // !!!: 往下拉正好 MKRefreshContentInsetSpacing 时会往回弹 10几点像素的问题处理
    if (self.panState && self.scrollView.insetBottom == 0 && self.scrollView.offsetY != MKRefreshContentInsetSpacing) {
        self.scrollView.offsetY = -MKRefreshContentInsetSpacing;
    }
}

- (void)scrollViewPanStateDidChange:(NSDictionary *)change{
    [super scrollViewPanStateDidChange:change];
    self.panState = [change[@"new"] integerValue] == 3;
    self.offsety = self.scrollView.contentOffset.y;

    if ([change[@"new"] integerValue] == 3 && self.scrollView.offsetY <= -MKRefreshContentInsetSpacing) {
        self.state = RefreshPulling;
    }
    

}

#pragma mark - 用户提供的View
- (void)setCustomHeader:(UIView<RefreshStatusProtocol> *)customHeader{
    if (_customHeader) {
        [_customHeader removeFromSuperview];
    }
    _customHeader = customHeader;
    _customHeader.frame = self.bounds;
    [self addSubview:_customHeader];
}


- (void)protocolSend{
    
    
    
    // !!!: 松开手-立即刷新
    if ((self.state == RefreshIdle && self.scrollView.offsetY <= -MKRefreshContentInsetSpacing) && !self.panState) {
        if ([self.customHeader respondsToSelector:@selector(viewWhenFreeReload)]) {
            [self.customHeader viewWhenFreeReload];
            return;
        }
    }
    
    // !!!: 正在往下拉动
    if (self.state == RefreshIdle && self.scrollView.offsetY < 0) {
        if ([self.customHeader respondsToSelector:@selector(viewPullingWithOffset:)]) {
            [self.customHeader viewPullingWithOffset:fabs(self.scrollView.offsetY) > MKRefreshContentInsetSpacing ? MKRefreshContentInsetSpacing : fabs(self.scrollView.offsetY)];
        }
    }
    
    // !!!: 正在刷新
    if (self.panState && (self.state == RefreshPulling || self.state == RefreshRefreshing)) {
        if ([self.customHeader respondsToSelector:@selector(viewReloading)]) {
            [self.customHeader viewReloading];
        }
    }
    
    NSLog(@"%ld=====%d",self.state,self.panState);
}

@end
